/**
 * YESBRAIN Javascript Library;
 * @author gongji
 */

	/*
	 * Insert SQL Language
	 * Can use basic insert language
	 */

YESBRAIN.JSQL.INSERT = {
	
		parseTable : function(sql){
		//var sql = "select * from test";
		var re = new RegExp(/\s*insert\s+into\s+(.+)\s*\(.+values/ig);
		var ree;
		var res;
		res = re.exec(sql);
		if(res) {
			return res[1];
		}
		else {
			return null;
		}
	},
	
	parseCols : function(sql) {
		var re = new RegExp(/\s*insert\s+into\s+.+\(\s*(.+)\s*\).*values/ig);
		//var sql = "select * from";
		var res;
		res = re.exec(sql);
		if(res) {
			return res[1];
		}
		else {
			return null;
		}
	},
	
	parseValues : function(sql) {
		var re = new RegExp(/.+values\s*\(\s*(.+)\s*\)/ig);
		//var sql = "select * from";
		var res;
		res = re.exec(sql);
		if(res) {
			return res[1];
		}
		else {
			return null;
		}
	},
	
	splitCols : function(cols) {
		var colsArr;
		var res;
		colsArr = cols.split(',');
		for(var i=0;i<colsArr.length;i++) {
			var re = new RegExp(/\s*(.+)\s*/ig);
			res = re.exec(colsArr[i]);
			if(res) {
				colsArr[i] = res[1];
			};
		};
		return colsArr;
	},
	
	splitValues : function(values) {
		var valueObjs = [];
		var valuesArr;
		var res;
		valuesArr = values.split(',');
		return valuesArr;
	},
	
	executeInsert : function(sql) {
		var table = this.parseTable(sql);
		table = eval(table);
		var cols = this.parseCols(sql);
		cols = this.splitCols(cols);
		var values = this.parseValues(sql);
		values = this.splitValues(values);
		if(cols.length!=values.length) {
			alert('cols or values format error!');
		};
		var row = {};
		for(var i=0;i<cols.length;i++) {
			row[cols[i]] = eval(values[i]);
		};
		table.push(row);
		return true;
	}
};